package com.ld.caodan.activity;


import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 
 * 欢迎页面
 * 
 * @author 刘笛 2011-10-19 下午02:41:28
 */
public class CDWelcomeActivity extends Activity {
	
	private int progress_ = 0;

	private TextView textView;
	private ProgressBar bar;
	Timer timer;
	private boolean isLoading = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		// 设定渐变开始动画，持续三秒钟
		LinearLayout linear = (LinearLayout) findViewById(R.id.logo_bg);
		AlphaAnimation alpha = new AlphaAnimation(0.1f, 1.0f);
		alpha.setDuration(3000);
		linear.startAnimation(alpha);
		isLoading = true;
		textView=(TextView)findViewById(R.id.welcome_text);
		bar=(ProgressBar)findViewById(R.id.welcome_bar);
		bar.setMax(100);
        //持续滚动
		timer=new Timer();
		timer.schedule(new TimerTask(){
            @Override
            public void run() {
            	handler.sendEmptyMessage(progress_+=3);
            }
        }, 0,200);
		
	}

	private String text1 = "这里是腐女的乐园";
	private String text2 = "更是宅男的福地";
	private String text3 = "尽情Happy吧 童鞋们!";
	Handler handler = new Handler(){
		
		public void handleMessage(android.os.Message msg) {
			int progress = msg.what;
			bar.setProgress(progress);
			if(isLoading){
				if(progress>20){
					if(!text1.equals(textView.getText().toString())){
						textView.setText(text1);
					}
				}
				if(progress>60){
					if(!text2.equals(textView.getText().toString())){
						textView.setText(text2);
					}
				}
				if(progress>78){
					if(!text3.equals(textView.getText().toString())){
						textView.setText(text3);
					}
				}
				if(progress>=100&&timer!=null){
					timer.cancel();
					Intent intent = new Intent(CDWelcomeActivity.this,
							TabHostActivity.class);
					startActivity(intent);
					finish();
				}
			}else{
				finish();
			}
		};
	};
	
	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
		if(KeyEvent.KEYCODE_BACK==keyCode){
			isLoading = false;
		}
		return true;
	};
}
